﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Instrumenter
{
    public partial class WinMain : Form
    {
        private static int p_TrackLength = 6000;
        private static int p_VolumeAttuenation = 6000;
        private Microsoft.DirectX.AudioVideoPlayback.Audio p_Player;
        
        private Journal p_Journal;

        public WinMain()
        {
            InitializeComponent();
            p_Player = null;
            TrackSlider.Maximum = p_TrackLength;
            TrackVolume.Maximum = p_VolumeAttuenation;
            TrackVolume.Value = p_VolumeAttuenation / 2;
            p_Journal = null;
        }

        private void ButtonChooseFile_Click(object sender, EventArgs e)
        {
            if (DialogOpen.ShowDialog() == DialogResult.OK)
            {
                if (p_Player != null)
                {
                    if (!p_Player.Stopped)
                        p_Player.Stop();
                }
                p_Player = new Microsoft.DirectX.AudioVideoPlayback.Audio(DialogOpen.FileName);
                p_Player.Volume = TrackVolume.Value - p_VolumeAttuenation;

                TextFile.Text = DialogOpen.FileName;
                TextPropertyLength.Text = p_Player.Duration.ToString() + " seconds";
                TrackSlider.Value = 0;

                p_Journal = new Journal();
                p_Journal.Name = DialogOpen.SafeFileName;
                p_Journal.AudioFile.LocalPath = DialogOpen.FileName;

                TreeSegments.Nodes.Clear();
                TreeSegments.Nodes.Add(new TreeNode(p_Journal.Name));
                TreeSegments.Nodes[0].Tag = p_Journal;
                TreeSegments.Nodes[0].Nodes.Add(new TreeNode("AudioFile"));
                TreeSegments.Nodes[0].Nodes.Add(new TreeNode("Meta"));
                TreeSegments.Nodes[0].Nodes[0].Tag = p_Journal.AudioFile;
                TreeSegments.Nodes[0].Nodes[1].Tag = p_Journal.Meta;

                p_Player.Play();
                TimerCheckPlayer.Enabled = true;
                ButtonPlay.Text = "Pause";
            }
        }

        private void ButtonPlay_Click(object sender, EventArgs e)
        {
            if (p_Player != null)
            {
                if (p_Player.Stopped || p_Player.Paused)
                {
                    p_Player.Play();
                    TimerCheckPlayer.Enabled = true;
                    ButtonPlay.Text = "Pause";
                }
                else
                {
                    p_Player.Pause();
                    TimerCheckPlayer.Enabled = false;
                    ButtonPlay.Text = "Play";
                }
            }
        }

        private void TrackSlider_Scroll(object sender, EventArgs e)
        {
            if (p_Player != null)
            {
                p_Player.CurrentPosition = System.Convert.ToDouble(TrackSlider.Value * p_Player.Duration / p_TrackLength);
            }
        }

        private void TrackVolume_Scroll(object sender, EventArgs e)
        {
            if (p_Player != null)
                p_Player.Volume = TrackVolume.Value - p_VolumeAttuenation;
        }

        private void TimerCheckPlayer_Tick(object sender, EventArgs e)
        {
            if (p_Player != null)
            {
                TrackSlider.Value = System.Convert.ToInt32(p_Player.CurrentPosition / p_Player.Duration * p_TrackLength);
            }
        }

        private void TreeSegments_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (TreeSegments.SelectedNode != null)
                PropertyGridSegment.SelectedObject = TreeSegments.SelectedNode.Tag;
            else
                PropertyGridSegment.SelectedObject = null;
        }

        private void MenuTreeNewIntroduction_Click(object sender, EventArgs e)
        {
            if (TreeSegments.SelectedNode != null && (TreeSegments.SelectedNode.Tag is Segment || TreeSegments.SelectedNode.Tag is Journal))
            {
                Introduction Introduction = new Introduction();
                Introduction.Name = "Introduction";

                if (TreeSegments.SelectedNode.Tag is Journal)
                {
                    p_Journal.AddChild(Introduction);
                }
                else
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    Current.AddChild(Introduction);
                }
                TreeSegments.SelectedNode.Nodes.Add(new TreeNode(Introduction.Name));
                TreeSegments.SelectedNode.LastNode.Tag = Introduction;
            }
        }

        private void MenuTreeNewReport_Click(object sender, EventArgs e)
        {
            if (TreeSegments.SelectedNode != null && (TreeSegments.SelectedNode.Tag is Segment || TreeSegments.SelectedNode.Tag is Journal))
            {
                Report Report = new Report();
                Report.Name = "Report";

                if (TreeSegments.SelectedNode.Tag is Journal)
                {
                    p_Journal.AddChild(Report);
                }
                else
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    Current.AddChild(Report);
                }
                TreeSegments.SelectedNode.Nodes.Add(new TreeNode(Report.Name));
                TreeSegments.SelectedNode.LastNode.Tag = Report;
                TreeSegments.SelectedNode.LastNode.Nodes.Add(new TreeNode("Reporter"));
                TreeSegments.SelectedNode.LastNode.LastNode.Tag = Report.Reporter;
            }
        } 

        private void MenuTreeNewSubject_Click(object sender, EventArgs e)
        {
            if (TreeSegments.SelectedNode != null && (TreeSegments.SelectedNode.Tag is Segment || TreeSegments.SelectedNode.Tag is Journal))
            {
                Subject Subject = new Subject();
                Subject.Name = "Subject";

                if (TreeSegments.SelectedNode.Tag is Journal)
                {
                    p_Journal.AddChild(Subject);
                }
                else
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    Current.AddChild(Subject);
                }
                TreeSegments.SelectedNode.Nodes.Add(new TreeNode(Subject.Name));
                TreeSegments.SelectedNode.LastNode.Tag = Subject;
            }
        }

        private void MenuTreeNewInterview_Click(object sender, EventArgs e)
        {
            if (TreeSegments.SelectedNode != null && (TreeSegments.SelectedNode.Tag is Segment || TreeSegments.SelectedNode.Tag is Journal))
            {
                Interview Interview = new Interview();
                Interview.Name = "Interview";

                if (TreeSegments.SelectedNode.Tag is Journal)
                {
                    p_Journal.AddChild(Interview);
                }
                else
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    Current.AddChild(Interview);
                }
                TreeSegments.SelectedNode.Nodes.Add(new TreeNode(Interview.Name));
                TreeSegments.SelectedNode.LastNode.Tag = Interview;
                TreeSegments.SelectedNode.LastNode.Nodes.Add(new TreeNode("Interviewer"));
                TreeSegments.SelectedNode.LastNode.LastNode.Tag = Interview.Interviewer;
                TreeSegments.SelectedNode.LastNode.Nodes.Add(new TreeNode("Interviewed"));
                TreeSegments.SelectedNode.LastNode.LastNode.Tag = Interview.Interviewed;
            }
        }

        private void MenuTreeDeleteChild_Click(object sender, EventArgs e)
        {
            if (TreeSegments.SelectedNode != null)
            {
                if (!(TreeSegments.SelectedNode.Tag is Segment))
                {
                    MessageBox.Show("Cannot delete this mandatory node !", "Instrumenter", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                if (MessageBox.Show("Delete segment " + Current.Name + " ?", "Instrumenter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;
                if (TreeSegments.SelectedNode.Parent.Tag is Journal)
                    p_Journal.RemoveChild(Current);
                else
                    ((Segment)TreeSegments.SelectedNode.Parent.Tag).RemoveChild(Current);
                TreeSegments.SelectedNode.Remove();
                PropertyGridSegment.SelectedObject = null;
                PropertyGridSegment.Refresh();
            }
        }

        private void LinkSetBegin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (p_Journal != null && p_Player != null && TreeSegments.SelectedNode != null)
            {
                if (TreeSegments.SelectedNode.Tag is Segment)
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    Current.PositionBegin = p_Player.CurrentPosition;
                    PropertyGridSegment.Refresh();
                }
            }
        }

        private void LinkSetEnd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (p_Journal != null && p_Player != null && TreeSegments.SelectedNode != null)
            {
                if (TreeSegments.SelectedNode.Tag is Segment)
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    Current.PositionEnd = p_Player.CurrentPosition;
                    PropertyGridSegment.Refresh();
                }
            }
        }

        private void LinkGotoBegin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (p_Journal != null && p_Player != null && TreeSegments.SelectedNode != null)
            {
                if (TreeSegments.SelectedNode.Tag is Segment)
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    p_Player.CurrentPosition = Current.PositionBegin;
                }
            }
        }

        private void LinkGotoEnd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (p_Journal != null && p_Player != null && TreeSegments.SelectedNode != null)
            {
                if (TreeSegments.SelectedNode.Tag is Segment)
                {
                    Segment Current = (Segment)TreeSegments.SelectedNode.Tag;
                    p_Player.CurrentPosition = Current.PositionEnd;
                }
            }
        }

        private void PropertyGridSegment_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if ((TreeSegments.SelectedNode != null) && (p_Player != null))
            {
                if (TreeSegments.SelectedNode.Tag is Journal)
                    TreeSegments.SelectedNode.Text = p_Journal.Name;
                else if (TreeSegments.SelectedNode.Tag is Segment)
                    TreeSegments.SelectedNode.Text = ((Segment)TreeSegments.SelectedNode.Tag).Name;
                TreeSegments.Refresh();
                PropertyGridSegment.Refresh();
            }
        }

        private void ButtonExport_Click(object sender, EventArgs e)
        {
            if (p_Journal != null)
            {
                System.Xml.XmlDocument Doc = new System.Xml.XmlDocument();
                Doc.AppendChild(Doc.CreateXmlDeclaration("1.0", "utf-8", "yes"));

                Doc.AppendChild(p_Journal.CreateXMLNode(Doc));
                
                if (DialogExport.ShowDialog() == DialogResult.OK)
                    Doc.Save(DialogExport.FileName);
            }
        }

         
    }
}
